package com.red.star.wechat.data.mappers;

import com.red.star.wechat.data.core.base.MyMapper;
import com.red.star.wechat.data.entity.ConditionChoose;
import com.red.star.wechat.data.entity.Employee;
import com.red.star.wechat.data.entity.bo.EmployeeBo;
import com.red.star.wechat.data.entity.vo.EmployeePartakeAnalyse;
import com.red.star.wechat.data.entity.vo.UserDetails;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface EmployeeMapper extends MyMapper<Employee> {
    List getEmployeeByMerchantId(String merchantId);

    List<Employee> getEmployeeListByGroupId(Integer groupId);

    List<Employee> getEmployeeByVipOpenId(String vipOpenId);

    List<Employee> getEmployeeByFansId(Integer fansID);

    List<Employee> getEmployeeByMallCode(String mallCode);

    List<Employee> getEmployeeByParam(Employee employee);

    public void updateEmployee(Employee employee);

    @Update(value = "update t_employee set status =2 where id = #{id}")
    public void deleteEmployeeById(Integer id);

    void updateEmployeeByParam(Employee employee);

    List<Employee> getAllEmployeeList(Employee employee);

    @Select(value = "select * from t_employee where vip_open_id = #{vipOpenId} and status = 0")
    Employee getEmployeeByVipOpenIdAndStatusO(String vipOpenId);

    @Select(value = "select * from t_employee t where t.vip_open_id = #{vipOpenId} and status = 0")
    public Employee findEmployeeByVipOpenid(String vipOpenId);

    @Select(value = "select * from t_employee where fans_id = #{fansId} and status = 0 limit 1")
    Employee getEmployeeBybFansIdAndStatusO(Integer fansId);

    @Select(value = "select * from t_employee where fans_id = #{fansId} and status = 0 and is_shop_owner = 1 limit 1")
    Employee getEmployeeBybFansIdAndStatusOAndIsShopOwner(Integer fansId);

    @Select(value = "select * from t_employee t where t.fans_id = #{fansId} and status = 0")
    public Employee findEmployeeByFansId(Integer fansId);

    @Select(value = "select * from t_employee t where t.vip_id = #{vipId}")
    public Employee findEmployeeByVipId(String vipId);

    //条件：1、status为0 2、merchant_id不为空且为当前导购员的一致 3、不包含当前导购员 4、type为0
    @Select(value = "select * from t_employee t where t.status = 0 and merchant_id = #{merchantId} and " +
            "t.type = 0 and id <> #{employeeId} and t.is_shop_owner = 1")
    public Employee findEffectiveEmployee(@Param("merchantId") Integer merchantId, @Param("employeeId") Integer employeeId);

    //条件：1、status为0 2、merchant_id不为空且为当前导购员的一致 4、type为0
    @Select(value = "select * from t_employee t where t.status = 0 and merchant_id = #{merchantId} and " +
            "t.type = 0 and t.is_shop_owner = 1")
    Employee findEffectiveEmployeeOther(@Param("merchantId") Integer merchantId);

    public void changeEmployeeById(@Param("id") Integer id, @Param("isShopOwner") Boolean isShopOwner);

    public void changeEmployeeByNameAndMobile(@Param("employeeName") String employeeName, @Param("employeeMobile") String employeeMobile, @Param("isShopOwner") Boolean isShopOwner);

    List<Employee> selectEmployeeListByParamNew(Employee employee);

    Employee findEmployeeSetting(@Param("mallCode") String mallCode);

    @Select("SELECT id, fans_id, name FROM t_employee WHERE is_customer = 1 AND `status` = 0 AND type = 1 AND mall_code = #{mallCode}")
    List<Employee> listEmployeeByMallCode(@Param("mallCode") String mallCode);


    Employee findEmployeeByMobile(@Param("mobile") String mobile, @Param("mallCode") String mallCode);

    public void updateEmployeeIsCustomerById(@Param("id") Integer id, @Param("isCustomer") Integer isCustomer, @Param("priority") Integer priority);

    public void updateEmployeeIsCustomerByMallCode(@Param("mallCode") String mallCode);

    @Select("select id, fans_id from t_employee where status = 0 and type = 0 and mall_code = #{mallCode}")
    List<Employee> findEmployeeByMallCode(@Param("mallCode") String mallCode);

    @Select("select * from t_employee where status = 0 and type = 0")
    List<Employee> findAllEmployee();

    Employee findEmployeeByMobileAndMallCode(@Param("mobile") String mobile, @Param("mallCode") String mallCode);

    List<EmployeePartakeAnalyse> listEmployeePartakeAnalyse(EmployeePartakeAnalyse employeePartakeAnalyse);

    List<Integer> findEmployeeByFansIds(@Param("list") List<Integer> list);

    List<Employee> findListEmployee(@Param("list") List<Integer> list);


    Integer addOrUpdate(Employee employee);

    Employee findEmployeeByMobileTow(@Param("mobile") String mobile, @Param("mallCode") String mallCode);

    @Select(value = "select te.* from t_merchant tm JOIN t_employee te on tm.id=te.merchant_id where tm.brand_name=#{brandName} and tm.market_name=#{marketName} and te.type=0 and te.status=0")
    List<Employee> findEmployeeByBrandname(@Param("brandName") String brandName, @Param("marketName") String marketName);

    Integer countEffectiveByCondition(Employee employee);

    @Select(value = "SELECT * FROM t_employee WHERE vip_open_id = #{vipOpenId}")
    List<Employee> findEmployeeByVipOpenidNotStatus(@Param("vipOpenId") String vipOpenId);

    int updateEmployeeMobileByIds(@Param("list") List<Integer> list, @Param("mobile") String mobile);

    Integer findEffectiveByCondition(Employee employee);

    @Select("SELECT COUNT(*) FROM t_employee WHERE is_shop_owner = 1 and id= #{employeeId}")
    int countIsShopOwnerForEmployee(@Param("employeeId") Integer employeeId);

    @Select(value = "select t.*,tm.oms_code,tme.shop_name,tf.open_id,tf.display_name,tme.shop_id from t_employee t left join t_mall tm on tm.mall_code = t.mall_code left join t_merchant tme on t.merchant_id = tme.id left join t_fans tf on tf.id = t.fans_id where t.id = #{id}")
    EmployeeBo findEmployeeById(@Param("id") Integer id);

    @Select(value = "select t.id from t_employee t where t.fans_id = #{id} and t.`status` = 0")
    Integer findIdByFansId(@Param("id") Integer id);

    UserDetails getEmployeeDetails(@Param("employeeId") Integer employeeId);

    @Select(value = "select t.* from t_employee t where t.mobile = #{mobile} and t.`status` = 0 and t.type=0 order by t.id desc limit 1")
    Employee findEmployeeOnlyByMobile(@Param("mobile") String mobile);

    int updateStatusById(@Param("id") Integer i, @Param("status") Integer status);

    List<ConditionChoose> findConditionChoose();

    @Select("SELECT open_id FROM t_fans where id = (select fans_id from t_employee where id= #{id})")
    String getOpenIdByEmployeeId(@Param("id") Integer employeeId);

    String findMerchantPosterMallCode(@Param("posterId") Integer posterId);

}
